package com.winai.launcher.view.set;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import androidx.annotation.Nullable;

import com.winai.launcher.R;
import com.winai.launcher.base.BaseActivity;
import com.winai.launcher.bean.WifiItemBean;
import com.winai.launcher.common.GlobalPublisher;
import com.winai.launcher.manager.WifiAutoConnectManager;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 作者：康少
 * 时间：2019/11/19 0019
 * 说明：连接wifi的具体操作
 */
public class SetWifiActionActivity extends BaseActivity {
    private Context mContext = this;

    private final int RESULT_CODE = 11000;

    @BindView(R.id.str_title)
    TextView strTitle;

    private String titleName;//wifi名称
    private WifiItemBean wifiItemBean;

    private WifiAutoConnectManager wifiAutoConnectManager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_set_wifi_action);
        ButterKnife.bind(this);
        start();
    }

    private void start() {
        initObject();
    }

    private void initObject() {
        WifiManager mWifiManager = (WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE);
        wifiAutoConnectManager = WifiAutoConnectManager.newInstance(mWifiManager);

        wifiItemBean = getIntent().getParcelableExtra("wifiItemBean");
        if (wifiItemBean != null) {
            if (!TextUtils.isEmpty(wifiItemBean.getWifiName())) {
                titleName = wifiItemBean.getWifiName();
                strTitle.setText(titleName);
            }
        }
    }

    private void cancelSave() {
        // 取消保存，忽略网络，忽略WiFi，忘记网络
        wifiAutoConnectManager.removeWifiBySsid(titleName);
        finish();
        GlobalPublisher.Notify(GlobalPublisher.CHANGE_WIFI, 0);
    }

    private void connectWifi() {
        Intent intent = new Intent();
        intent.putExtra("wifiItemBean", wifiItemBean);
        setResult(Activity.RESULT_OK, intent);
        finish();
    }

    @OnClick({R.id.iv_back, R.id.iv_home, R.id.tv_connectWifi, R.id.tv_cancelSave})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_back:
                finish();
                break;
            case R.id.iv_home:
                //模拟HOME
                Intent it = new Intent(Intent.ACTION_MAIN);
                it.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                it.addCategory(Intent.CATEGORY_HOME);
                mContext.startActivity(it);
                break;
            case R.id.tv_connectWifi:
                //连接wifi
                connectWifi();
                break;
            case R.id.tv_cancelSave:
                //取消保存
                cancelSave();
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == Activity.RESULT_OK) {
            switch (requestCode) {
                case RESULT_CODE:
                    if (data != null) {
                        boolean finishAct = data.getBooleanExtra("finishAct", false);
                        if (finishAct) {
                            finish();
                        }
                    }

                    break;
            }
        }
    }
}
